import { type Model } from 'mongoose';
import { CreateRoleDto, Role, RoleDocument, UpdateRoleDto } from '@ai/entity';
import { Injectable, OnModuleInit } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { ROLE_KEY, ROLE_MENU } from '@/common/config/role.menu';

@Injectable()
export class RoleService implements OnModuleInit {
    constructor(@InjectModel(Role.name) private readonly roleModel: Model<RoleDocument>) {}

    onModuleInit() {
        this.roleModel.init();
    }

    create(createRoleDto: CreateRoleDto) {
        return this.roleModel.create(createRoleDto);
    }

    findAll() {
        return this.roleModel
            .find()
            .populate('permMenu', '', 'PermMenu')
            .populate('permAction', '', 'PermAction')
            .exec();
    }

    findOneById(id: string) {
        return this.roleModel
            .findById(id)
            .populate('permMenu', '', 'PermMenu')
            .populate('permAction', '', 'PermAction')
            .exec();
    }

    update(id: string, updateRoleDto: UpdateRoleDto) {
        return this.roleModel.findByIdAndUpdate(id, updateRoleDto);
    }

    remove(id: string) {
        return this.roleModel.findByIdAndDelete(id);
    }
}
